package org.ws.task.netty.manager.support;

import com.google.common.collect.Maps;
import io.netty.channel.Channel;
import org.ws.task.netty.manager.ChannelConnectManager;

import java.util.Collection;
import java.util.Map;

public class DefaultChannelConnectManager implements ChannelConnectManager {

    private Map<String, Channel> channelMap = Maps.newConcurrentMap();

    @Override
    public Channel getChannel(String remote) {
        if(channelMap.containsKey(remote)){
            return channelMap.get(remote);
        }
        return null;
    }

    @Override
    public Collection<Channel> channels() {
        return channelMap.values();
    }

    @Override
    public void onConnect(String remote, Channel channel) {
        channelMap.put(remote,channel);
    }

    @Override
    public void onClose(String remote, Channel channel) {
        channelMap.remove(remote);
    }

    @Override
    public void onException(String remote, Channel channel) {
        channelMap.remove(remote);
    }

    @Override
    public void onIdle(String remote, Channel channel) {
        channelMap.remove(remote);
    }
}
